<?php
class SLS_BoEditRight extends SLS_BoControllerProtected 
{
	
	public function action() 
	{		
		// Objects
		$xml = $this->getXML();
		$user = $this->hasAuthorative();
		$xml = $this->makeMenu($xml);
			
		$name = SLS_String::trimSlashesFromString($this->_http->getParam("name"));				
		$pathsHandle = file_get_contents($this->_generic->getPathConfig("configSls")."/rights.xml");
		$xmlRights = new SLS_XMLToolbox($pathsHandle);			
		$result = $xmlRights->getTags("//sls_configs/entry[@login='".($name)."']");
		
		if (empty($result))
		{
			$xml->startTag("errors");
			$xml->addFullTag("error","This account doesn't exists",true);
			$xml->endTag("errors");
		}
		else
		{
			$login 	= $name;
			$pwd 	= "";
			$actions = array("read"   => array(),
						 "add" 	  => array(),
						 "edit"   => array(),
						 "delete" => array(),
						 "clone"  => array(),
						 "email" => array(),
						 "custom" => array());
		
			$results = $this->_generic->getControllersXML()->getTagsAttributes("//controllers/controller[@isBo='true']/scontrollers/scontroller",array("name","id"));
			for($i=0 ; $i<$count=count($results) ; $i++)
			{
				$name = $results[$i]["attributes"][0]["value"];
				$aid = $results[$i]["attributes"][1]["value"];
				switch($name)
				{
					case (SLS_String::startsWith($name,"List") && SLS_String::contains($name,"_")):
						$actions["read"][$aid] = $name;		
						break;
					case (SLS_String::startsWith($name,"Add") && SLS_String::contains($name,"_")):
						$actions["add"][$aid] = $name;		
						break;
					case (SLS_String::startsWith($name,"Modify") && SLS_String::contains($name,"_")):
						$actions["edit"][$aid] = $name;		
						break;
					case (SLS_String::startsWith($name,"Delete") && SLS_String::contains($name,"_")):
						$actions["delete"][$aid] = $name;		
						break;
					case (SLS_String::startsWith($name,"Clone") && SLS_String::contains($name,"_")):
						$actions["clone"][$aid] = $name;		
						break;
					case (SLS_String::startsWith($name,"Email") && SLS_String::contains($name,"_")):
						$actions["email"][$aid] = $name;		
						break;
				}
			}
			
			$translationId = array_shift($this->_generic->getControllersXML()->getTags("//controllers/controller[@isBo='true']/scontrollers/scontroller[@name='Translation']/@id"));
			if (!empty($translationId))
				$actions["custom"][$translationId] = "_Translation";
			$fileuploadId = array_shift($this->_generic->getControllersXML()->getTags("//controllers/controller[@isBo='true']/scontrollers/scontroller[@name='FileUpload']/@id"));
			if (!empty($fileuploadId))
			$actions["custom"][$fileuploadId] = "_FileUpload";
						
			$xml->startTag("bo_groups");
			foreach($actions as $action => $values)
			{
				$xml->startTag("bo_group");
				$xml->addFullTag("name",$action,true);
				foreach($values as $aid => $name)
				{
					$model = "";
					$selected = array_shift($xmlRights->getTags("//sls_configs/entry[@login='".($login)."']/action[@id='".$aid."']/@role"));
										
					switch ($name)
					{
						case (SLS_String::startsWith($name,"List")): 	$model = SLS_String::substrBeforeFirstDelimiter(SLS_String::substrAfterFirstDelimiter($name,"List"),"_"); 	break;
						case (SLS_String::startsWith($name,"Add")): 	$model = SLS_String::substrBeforeFirstDelimiter(SLS_String::substrAfterFirstDelimiter($name,"Add"),"_"); 	break;
						case (SLS_String::startsWith($name,"Modify")): 	$model = SLS_String::substrBeforeFirstDelimiter(SLS_String::substrAfterFirstDelimiter($name,"Modify"),"_"); break;
						case (SLS_String::startsWith($name,"Delete")): 	$model = SLS_String::substrBeforeFirstDelimiter(SLS_String::substrAfterFirstDelimiter($name,"Delete"),"_"); break;
						case (SLS_String::startsWith($name,"Clone")): 	$model = SLS_String::substrBeforeFirstDelimiter(SLS_String::substrAfterFirstDelimiter($name,"Clone"),"_"); 	break;
						case (SLS_String::startsWith($name,"Email")): 	$model = SLS_String::substrBeforeFirstDelimiter(SLS_String::substrAfterFirstDelimiter($name,"Email"),"_"); 	break;
						default:										$model = "";
					}
					$xml->startTag("action");
						$xml->addFullTag("name",SLS_String::substrAfterFirstDelimiter($name,"_"),true);
						$xml->addFullTag("model",$model,true);
						$xml->addFullTag("id",$aid,true);
						$xml->addFullTag("selected",(!empty($selected)) ? "true" : "false",true);
					$xml->endTag("action");
				}
				$xml->endTag("bo_group");
			}
			$xml->endTag("bo_groups");
						
			if ($this->_http->getParam("reload") == "true")
			{
				$errors = array();
				
				$login 	= SLS_String::trimSlashesFromString($this->_http->getParam("login"));
				$pwd 	= SLS_String::trimSlashesFromString($this->_http->getParam("password"));
				
				if (empty($login) || empty($pwd))
				{					
					array_push($errors,"You must fill the login and the password");					
				}
				
				if (empty($errors))
				{	
					$xmlRights->deleteTags('//sls_configs/entry[@login="'.($login).'"]');
					$xmlRights->saveXML($this->_generic->getPathConfig("configSls")."/rights.xml",$xmlRights->getXML());
					$pathsHandle = file_get_contents($this->_generic->getPathConfig("configSls")."/rights.xml");
					$xmlRights = new SLS_XMLToolbox($pathsHandle);
					$params = $this->_http->getParams();
															
					$xmlNew = '<entry login="'.($login).'" password="'.sha1($pwd).'">'."\n";
					
					foreach($params as $key => $value)
					{
						if (SLS_String::startsWith($key,"bo_action"))
						{
							$xmlNew .= '    <action id="'.$value.'" role="'.SLS_String::substrBeforeFirstDelimiter(SLS_String::substrAfterFirstDelimiter($key,"bo_action_"),"_").'" entity="'.((SLS_String::substrBeforeFirstDelimiter(SLS_String::substrAfterFirstDelimiter($key,"bo_action_"),"_") == "custom") ? "" : SLS_String::substrAfterFirstDelimiter(SLS_String::substrAfterFirstDelimiter($key,"bo_action_"),"_")).'" />'."\n";
						}
					}
					
					$xmlNew .= '  </entry>';				
					$xmlRights->appendXMLNode("//sls_configs",$xmlNew);
					$xmlRights->saveXML($this->_generic->getPathConfig("configSls")."/rights.xml",$xmlRights->getXML());
					$this->_generic->redirect("Manage/Rights");
				}
				else
				{
					$xml->startTag("errors");
					foreach($errors as $error)
						$xml->addFullTag("error",$error,true);
					$xml->endTag("errors");
				}
			}
						
			$xml->addFullTag("login",$login,true);
		}
		$this->saveXML($xml);
	}
	
}
?>